//
//  BAKit_SortView.m
//  BASorterSelectedView
//  排序
//  Created by Mr_zhang on 2017/9/13.
//  Copyright © 2017年 Mr_zhang. All rights reserved.
//

#import "BAKit_SortView.h"
#import "BAKit_SortModel.h"
#import "BASorterSelectedView.h"

#import <MJExtension.h>

@interface BAKit_SortView() <UITableViewDataSource,UITableViewDelegate>
/** 左边列表 */
@property (nonatomic, strong) UITableView *leftTableView;
/** 右边列表 */
@property (nonatomic, strong) UITableView *rightTableView;
/** 暂存行业类别、职能类别按钮 */
@property (nonatomic, strong) UIButton *selectedBtn;
/** 一级数据源 */
@property (nonatomic, strong) NSMutableArray *superAreas;
/** 二级数据源 */
@property (nonatomic, strong) NSMutableArray *subViewAreas;
/** 保存是否展开列表的key */
@property (nonatomic, strong) NSMutableDictionary *mutaDict;
/** 保存点击leftTableView的分区行 */
@property (nonatomic, assign) NSInteger leftTableViewRow;

@end

@implementation BAKit_SortView

- (NSMutableArray *)superAreas
{
    if(!_superAreas)
    {
        // 从 txt 文件中获取 json 数据
        NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BASorterSelectedData" ofType:@"txt"]];
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
        
        NSArray *array = dict[@"data"];
        
        // 解析成模型
        NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < array.count; i ++)
        {
            BAKit_SortModel *model = [BAKit_SortModel mj_objectWithKeyValues:array[i]];
            [mutableArray addObject:model];
        }
        _superAreas = [mutableArray copy];
    }
    return _superAreas;
}

- (NSMutableArray *)subViewAreas
{
    if (!_subViewAreas)
    {
        _subViewAreas = [[NSMutableArray alloc] init];
        for (int i = 0; i < self.superAreas.count; i++) {
            [_subViewAreas addObject:[NSArray new]];
        }
    }
    return _subViewAreas;
}

- (NSMutableDictionary *)mutaDict
{
    if (!_mutaDict)
    {
        _mutaDict = [[NSMutableDictionary alloc] init];
    }
    return _mutaDict;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUpSubView];
        
        // 调用行业、职能类别查询接口
        //[self checkJobRequireSecondLevel:@"industry" checkID:@"0"];
    }
    return self;
}

- (void)setUpSubView
{
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BAKit_SCREEN_WIDTH / 3, self.frame.size.height) style:UITableViewStylePlain];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.leftTableView];
    
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(BAKit_SCREEN_WIDTH / 3, 0 ,(BAKit_SCREEN_WIDTH / 3) * 2, self.frame.size.height) style:UITableViewStylePlain];
  //  NSLog(@"%f",self.rightTableView.sectionFooterHeight);
  //  self.rightTableView.sectionFooterHeight = 0; //分区尾部的高度（设置UITableView的样式为Grouped时，分区尾部存在高度）
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:self.rightTableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.leftTableView == tableView)
        return self.superAreas.count;
    else
    {
        BAKit_SortModel *superModel = self.superAreas[_leftTableViewRow];
        return superModel.subViewArea.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (tableView == self.leftTableView)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell"];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"leftCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.frame = CGRectMake(10, 12, self.leftTableView.frame.size.width, 20);
        [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        leftBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [leftBtn setTitleColor:BAKit_Color_Green_pod forState:UIControlStateSelected];
        leftBtn.contentHorizontalAlignment = NSTextAlignmentLeft;
        leftBtn.tag = indexPath.row;

        [leftBtn addTarget:self action:@selector(typeClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:leftBtn];
        
        BAKit_SortModel *superModel = self.superAreas[indexPath.row];
        [leftBtn setTitle:superModel.superArea forState:UIControlStateNormal];
        if (indexPath.row == 0)
        {
            leftBtn.selected = YES;
            self.selectedBtn = leftBtn;
            [self typeClick:leftBtn];
        }
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell"];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rightCell"];
            cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        }
        
        BAKit_SortModel *superModel = self.superAreas[self.leftTableViewRow];
        cell.textLabel.text = superModel.subViewArea[indexPath.row].name;
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.textLabel.textColor = [UIColor grayColor];
    }
    
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if(self.rightTableView == tableView)
//    {
//        if ([jobType isEqualToString:@"industry"])
//        {
//            if (self.jobTypeBlock)
//            {
//                NSString *jobType = [[self.subviewDataArray[indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
//                NSString *jobTypeId = [[self.subviewDataArray[indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"id"];
//                self.jobTypeBlock(jobType, jobTypeId,@"industryCategory");
//            }
//        }
//        else if ([jobType isEqualToString:@"function"])
//        {
//            UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
//            back.title = @"返回";
//            [self viewController].navigationItem.backBarButtonItem = back;
//            
//            AllJobTypeViewController *allJobType = [[AllJobTypeViewController alloc] init];
//            allJobType.title = @"职位列表";
//            allJobType.typeId = [[self.subviewDataArray[indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"id"];
//            allJobType.name = [[self.subviewDataArray[indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
//            __weak typeof(self) weakSelf = self;
//            allJobType.allJobTypeBlcok = ^(NSString *jobType, NSString *jobTypeId) {
//                weakSelf.jobTypeBlock(jobType, jobTypeId,@"functionCategory");
//            };
//            [[self viewController].navigationController pushViewController:allJobType animated:YES];
//        }
//    }
//    else
//        self.leftTableViewRow = indexPath.row;
//}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (self.rightTableView == tableView)
//    {
//        UIButton *sectionSelectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        sectionSelectedBtn.frame = CGRectMake(0, 0, self.frame.size.width, 44);
//        [sectionSelectedBtn setImage:[UIImage imageNamed:@"choose_02.png"] forState:UIControlStateNormal];
//        [sectionSelectedBtn setImage:[UIImage imageNamed:@"choose_select"] forState:UIControlStateSelected];
//        [sectionSelectedBtn setTitle:[self.dataArray[section] objectForKey:@"name"] forState:UIControlStateNormal];
//        sectionSelectedBtn.titleLabel.font = [UIFont systemFontOfSize:13];
//        [sectionSelectedBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        sectionSelectedBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        
//        [sectionSelectedBtn setImageEdgeInsets:UIEdgeInsetsMake(0, self.rightTableView.frame.size.width - 20, 0, 10)];
//        [sectionSelectedBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
//        [sectionSelectedBtn addTarget:self action:@selector(openSubViewList:) forControlEvents:UIControlEventTouchUpInside];
//        sectionSelectedBtn.tag = section;
//        
//        __weak typeof(self) weakSelf = self;
//        self.openCellBlock = ^(NSInteger sectionNum){
//            if (self.rightTableView == tableView)
//            {
//                if (![[weakSelf.dataArray[sectionNum] objectForKey:@"name"] isEqualToString:@"全部"])
//                {
//                    keyNum = sectionNum;
//                    NSString *key = [NSString stringWithFormat:@"%@",@(sectionNum)];
//                    if (![weakSelf.mutaDict objectForKey:key])
//                    {
//                        [weakSelf.mutaDict setObject:@"0" forKey:key];
//                        NSString *checkID = [[weakSelf.dataArray objectAtIndex:sectionNum] objectForKey:@"id"];
//                        [weakSelf checkJobRequireThirdLevel:jobType checkID:checkID];
//                    }
//                    else
//                    {
//                        [weakSelf.mutaDict removeObjectForKey:key];
//                        [weakSelf.subviewDataArray replaceObjectAtIndex:keyNum withObject:[NSArray new]];
//                        [weakSelf.rightTableView reloadData];
//                    }
//                }
//                else
//                {
//                    if (weakSelf.leftTableViewRow == 0)
//                        weakSelf.jobTypeBlock(@"全部", nil, @"industryCategory");
//                    else if (weakSelf.leftTableViewRow == 1)
//                        weakSelf.jobTypeBlock(@"全部", nil, @"functionCategory");
//                }
//            }
//        };
//        
//        return sectionSelectedBtn;
//    }
//    else
//        return nil;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    if (self.rightTableView == tableView)
//        return 33;
//    else
//        return 0;
//}

- (void)openSubViewList:(UIButton *)sender
{
//    self.openCellBlock(sender.tag);
//    NSLog(@"sender.tag:%ld",sender.tag);
    // [self checkJobRequireThirdLevel:jobType checkID:[self.dataArray[sender.tag] objectForKey:@"id"]];
}

- (void)typeClick:(UIButton *)sender
{
    self.leftTableViewRow = sender.tag;
    
    self.selectedBtn.selected = NO;
    sender.selected = YES;
    self.selectedBtn = sender;
    
    [self.rightTableView reloadData];
}

@end
